Welcome Guest | Sign in | Register
Data Files - C Programming Interview Questions and Answers | LucentBlackBoard | LucentBlackBoard.com

Home > Technical Interviews > Computer Science & Engineering > C Programming > Data Files Questions and Answers

1. If errno contains a nonzero number, is there an error?

The global variable errno is used by many standard C library functions to pass back to your program an error code that denotes specifically which error occurred. However, your program should not check the value of errnoto determine whether an error occurred.
Usually, the standard C library function you are calling returns with a return code which denotes that an error has occurred and that the value of errno has been set to a specific error number. If no error has occurred or if you are using a library function that does not reference errno, there is a good chance that errno will contain an erroneous value. For performance enhancement, the errno variable is sometimes not cleared by the functions that use it.
You should never rely on the value of errno alone; always check the return code from the function you are calling to see whether errno should be referenced. Refer to your compiler's library documentation for references to functions that utilize the errno global variable and for a list of valid values for errno.

2. What is a stream?

A stream is a continuous series of bytes that flow into or out of your program. Input and output from devices such as the mouse, keyboard, disk, screen, modem, and printer are all handled with streams. In C, all streams appear as files - not physical disk files necessarily, but rather logical files that refer to an input/output source. The C language provides five "standard" streams that are always available to your program. These streams do not have to be opened or closed. These are the five standard streams:


Name     Description           Example
stdin -    Standard Input -     Keyboard
stdout -  Standard Output -  Screen
stderr -  Standard Error -      Screen
stdprn - Standard Printer -    LPT1: port
stdaux - Standard Auxiliary - COM1: port

Note that the stdprn and stdaux streams are not always defined. This is because LPT1: and COM1: have no meaning under certain operating systems. However, stdin, stdout, and stderr are always defined. Also, note that the stdin stream does not have to come from the keyboard; it can come from a disk file or some other device through what is called redirection. In the same manner, the stdout stream does not have to appear on-screen; it too can be redirected to a disk file or some other device. See the next FAQ for an explanation of redirection.

3. How do you redirect a standard stream?

Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen, it can be redirected to a file or printer port. Similarly, your program's input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, < and >. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt:
C:>PRINTIT < STRINGS.TXT
Notice that the name of the executable file always comes first. The less-than sign (<) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program.
Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file namedOUTPUT.TXT, you would implement the freopen() function as shown here:
...
freopen("output.txt", "w", stdout);
...
Now, every output statement (printf(), puts(), putch(), and so on) in your program will appear in the fileOUTPUT.TXT.

4. How can you restore a redirected standard stream?

The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state.
The dup() function duplicates a file handle. You can use the dup() function to save the file handle corresponding to the stdout standard stream. The fdopen() function opens a stream that has been duplicated with the dup() function. Thus, as shown in the following example, you can redirect standard streams and restore them:
#include
void main(void);

void main(void)
{
int orig_stdout;
/* Duplicate the stdout file handle and store it in orig_stdout. */
orig_stdout = dup(fileno(stdout));
/* This text appears on-screen. */
printf("Writing to original stdout...\n");
/* Reopen stdout and redirect it to the "redir.txt" file. */
freopen("redir.txt", "w", stdout);
/* This text appears in the "redir.txt" file. */
printf("Writing to redirected stdout...\n");
/* Close the redirected stdout. */
fclose(stdout);
/* Restore the original stdout and print to the screen again. */
fdopen(orig_stdout, "w");
printf("I'm back writing to the original stdout.\n");
}

5. Can stdout be forced to print somewhere other than the screen?

Although the stdout standard stream defaults to the screen, you can force it to print to another device using something called redirection. For instance, consider the following program:
/* redir.c */
#include
void main(void);
void main(void)
{
printf("Let's get redirected!\n");
}
At the DOS prompt, instead of entering just the executable name, follow it with the redirection character >, and thus redirect what normally would appear on-screen to some other device. The following example would redirect the program's output to the prn device, usually the printer attached on LPT1:
C:>REDIR > PRN
Alternatively, you might want to redirect the program's output to a file, as the following example shows:
C:>REDIR > REDIR.OUT
In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT.




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.